package com.bookmall.member.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bookmall.common.utils.PageUtils;
import com.ruoyi.common.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.bookmall.member.domain.MemberLikeBooks;
import com.bookmall.member.service.IMemberLikeBooksService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 会员收藏Controller
 *
 * @author 胡菜鸡
 * @date 2022-04-01
 */
@RestController
@RequestMapping("/member/likes")
public class MemberLikeBooksController extends BaseController {
    @Autowired
    private IMemberLikeBooksService memberLikeBooksService;

    /**
     * 查询会员收藏列表
     */
    @PreAuthorize("@ss.hasPermi('member:likes:list')")
    @GetMapping("/list")
    public AjaxResult list(MemberLikeBooks memberLikeBooks)
    {
        PageUtils pageUtils = memberLikeBooksService.getMemberLikesWithMemberInfo(memberLikeBooks);
        return AjaxResult.success(pageUtils);
    }

    /**
     * 导出会员收藏列表
     */
    @PreAuthorize("@ss.hasPermi('member:likes:export')")
    @Log(title = "会员收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberLikeBooks memberLikeBooks)
    {
        List<MemberLikeBooks> list = memberLikeBooksService.list(new QueryWrapper<>(memberLikeBooks));
        ExcelUtil<MemberLikeBooks> util = new ExcelUtil<MemberLikeBooks>(MemberLikeBooks.class);
        util.exportExcel(response, list, "会员收藏数据");
    }

    /**
     * 获取会员收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:likes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(memberLikeBooksService.getById(id));
    }

    /**
     * 新增会员收藏
     */
    @PreAuthorize("@ss.hasPermi('member:likes:add')")
    @Log(title = "会员收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberLikeBooks memberLikeBooks)
    {
        try {
            int result = memberLikeBooksService.saveMemberLikesBook(memberLikeBooks);
            return toAjax(result);
        } catch (ServiceException e){
            System.out.println(e.getStackTrace());
            return AjaxResult.error();
        }
    }

    /**
     * 修改会员收藏
     */
    @PreAuthorize("@ss.hasPermi('member:likes:edit')")
    @Log(title = "会员收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberLikeBooks memberLikeBooks)
    {
        return toAjax(memberLikeBooksService.update(new QueryWrapper<>(memberLikeBooks)));
    }

    /**
     * 删除会员收藏
     */
    @PreAuthorize("@ss.hasPermi('member:likes:remove')")
    @Log(title = "会员收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(memberLikeBooksService.removeBatchByIds(ids));
    }
}
